import { ElMessage } from "element-plus";
import { defineStore } from "pinia";
import centerApi, { type MemberModel, type UserInfoModel } from "@/api/center";
const resetUserInfo = (): UserInfoModel => ({
   data: {
      account: "",
      avatar: "",
      level: 0,
      statisticsBackup: {
         followCount: 0,
         fansCount: 0,
      },
      avocation: [],
      channel: {
         groupType: "",
      },
      moreBackup: {
         skillLevel: 0,
         birthDate: "",
         sex: "",
         skillLevelName: "",
         city: "",
      },
   },
});
const resetMember = (): MemberModel => ({
   data: {
      channel: "",
      createTime: 0,
      data: {
         total_price: 0,
         time_limit: true,
         vip_grade: 0,
         end_time: "",
         title: "",
         duration: 0,
         apple_priceId: "",
         cover: "",
         start_time: "",
         unit: "",
         renew_time: "",
         price: 0,
         isYouthOnly: true,
         recom: false,
      },
      groupName: "",
      groupType: "",
      id: "",
      sortNum: 0,
      status: "",
      subGroupName: "",
      subGroupType: "",
      updateTime: 0,
      version: "",
   },
});
export const useUserStore = defineStore({
   id: "user",
   state: () => {
      return {
         // 个人信息
         userInfo: resetUserInfo(),
         // 技能水平
         groupType: "",
         // 会员信息
         memberData: resetMember(),
         skillLevel: "",
      };
   },
   actions: {
      // 获取用户信息
      async getUserInfo() {
         try {
            let result = await centerApi.getInfo();
            this.userInfo = result.data; //个人信息
            this.groupType = result.data.data.channel.groupType; //技能水平
            console.log("个人信息无data", result);

            console.log("个人信息", result.data);
         } catch (error) {
            ElMessage.error("获取用户数据失败");
         }
      },
      // 获取技能水平的数据
      async getPeraonal() {
         try {
            let result = await centerApi.getPeraonalData();
            console.log("技能水平", result);

            this.skillLevel = result.data;
         } catch (error) {
            ElMessage.error("获取技能水平数据失败");
         }
      },
      // 会员信息
      async getMember() {
         try {
            let result = await centerApi.getMember();
            console.log("会员信息", result.data);
            this.memberData = result.data;
         } catch (error) {
            ElMessage.error("获取会员信息失败");
         }
      },
      // 个人资料信息
      async getPeraonalData() {
         try {
            let result = await centerApi.getPeraonalData();
            console.log("个人资料信息", result.data);
            this.groupType = result.data;
         } catch (error) {
            ElMessage.error("获取个人资料信息失败");
         }
      },
   },
   getters: {
      //计算关注
      followCount: (state) => {
         return (state.userInfo.data.statisticsBackup || {}).followCount;
      },
      // 计算粉丝
      fansCount: (state) => {
         return (state.userInfo.data.statisticsBackup || {}).fansCount;
      },
      avocation(state) {
         return state.userInfo.data.avocation;
      },
   },
});
